package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation("角色分页")
    public ResponseResult<PageResponse<RoleVo>> getPage(@PathVariable Integer pageNum,
                                                        @PathVariable Integer pageSize, @RequestBody RoleDto roleDto) {
        PageResponse<RoleVo> roleVoPageResponse = roleService.getPageVo(pageNum, pageSize, roleDto);
        return ResponseResult.success(roleVoPageResponse);
    }

    @PutMapping
    @ApiOperation("角色添加")
    public ResponseResult addRole(@RequestBody RoleDto roleDto) {
        roleService.addRole(roleDto);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation("角色修改")
    public ResponseResult updateRole(@RequestBody RoleDto roleDto) {
        roleService.updateRole(roleDto);
        return ResponseResult.success();
    }

    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation("根据角色查询选中的资源数据")
    public ResponseResult<List<String>> selectResourceByRoleId(@PathVariable Long roleId) {
        List<String> resourceNo = roleService.selectResourceByRoleId(roleId);
        return ResponseResult.success(resourceNo);
    }


}
